package de.hm.edu.cep;

import com.espertech.esper.client.EPServiceProvider;
import com.espertech.esper.client.EPServiceProviderManager;
import com.espertech.esper.client.EPStatement;

/** 
 * Singleton class to manage event processing
 * 
 * @author Peter Goldenbogen (Munich University of Applied Science)
 * @version 1.0 
 */
public class EventEngine {
	
	private static EventEngine instance;
	static EPServiceProvider eps;
	
	private EventEngine(){}
	
	public static void initEE(){
		
		if(instance == null){
			instance = new EventEngine();
		}
		
		eps = EPServiceProviderManager.getDefaultProvider();
		
		//KPI Events
		String kpiExpression = "SELECT * FROM de.hm.edu.cep.KPIEvent.win:time(10 min) HAVING SUM(amount) > 10000 output first every 10 min";
		EPStatement kpiStmt = eps.getEPAdministrator().createEPL(kpiExpression);
		KPIEventListener kpiListener = new KPIEventListener();
		kpiStmt.addListener(kpiListener);

		// Order Events "SELECT * FROM de.hm.edu.cep.OrderEvent.win:length(2) HAVING AVG(amount) > 500"
		String orderExpression = "SELECT * FROM de.hm.edu.cep.OrderEvent";
		EPStatement orderStmt = eps.getEPAdministrator().createEPL(orderExpression);
		OrderEventListener oListener = new OrderEventListener();
		orderStmt.addListener(oListener);
		
	}

	public static EventEngine getInstance() {
		return instance;
	}

	public static EPServiceProvider getEps() {
		return eps;
	}
}
